CAUCHY
Overview
The CAUCHY function computes statistical properties of the Cauchy distribution, a continuous probability distribution also known as the Lorentz distribution in physics. Named after mathematician Augustin-Louis Cauchy, this distribution describes phenomena with heavy tails, including the ratio of two independent normally distributed random variables with mean zero.
This implementation uses SciPy’s scipy.stats.cauchy module, providing methods for PDF, CDF, inverse CDF (quantile function), survival function, and descriptive statistics.
The probability density function (PDF) of the Cauchy distribution is:
f(x; x_0, \gamma) = \frac{1}{\pi \gamma \left[1 + \left(\frac{x - x_0}{\gamma}\right)^2\right]}
where x_0 is the location parameter (specifying the peak of the distribution) and \gamma > 0 is the scale parameter (the half-width at half-maximum). For the standard Cauchy distribution (x_0 = 0, \gamma = 1), this simplifies to:
f(x) = \frac{1}{\pi(1 + x^2)}
The cumulative distribution function (CDF) is:
F(x; x_0, \gamma) = \frac{1}{\pi} \arctan\left(\frac{x - x_0}{\gamma}\right) + \frac{1}{2}
The Cauchy distribution is notable as a “pathological” distribution in statistics because its mean and variance are undefined—the integrals required to compute these moments do not converge. This makes it a classic counterexample in probability theory, demonstrating that the law of large numbers and central limit theorem require finite variance. The distribution’s median and mode are both equal to x_0.
The Cauchy distribution is equivalent to the Student’s t-distribution with one degree of freedom, and is one of only three stable distributions with closed-form PDFs (the others being the normal and Lévy distributions). In physics, it describes spectral line shapes subject to homogeneous broadening (Breit-Wigner distribution) and is used in computational finance to model fat-tailed distributions for value-at-risk calculations.
This example function is provided as-is without any representation of accuracy.
Excel Usage
=CAUCHY(value, loc, scale, cauchy_method)
value(float, optional, default: null): Input value for PDF/CDF/SF, or probability for ICDF/ISF methodsloc(float, optional, default: 0): Location parameter for shifting the distributionscale(float, optional, default: 1): Scale parameter for stretching the distribution (must be > 0)cauchy_method(str, optional, default: “pdf”): Statistical method to compute
Returns (float): Distribution result (float), or error message string.
Examples
Example 1: PDF at x=2 with standard parameters
Inputs:
| value | loc | scale | cauchy_method |
|---|---|---|---|
| 2 | 0 | 1 |
Excel formula:
=CAUCHY(2, 0, 1, "pdf")
Expected output:
0.06366
Example 2: CDF at x=2 with standard parameters
Inputs:
| value | loc | scale | cauchy_method |
|---|---|---|---|
| 2 | 0 | 1 | cdf |
Excel formula:
=CAUCHY(2, 0, 1, "cdf")
Expected output:
0.8524
Example 3: Inverse CDF (quantile) at q=0.85242
Inputs:
| value | loc | scale | cauchy_method |
|---|---|---|---|
| 0.85242 | 0 | 1 | icdf |
Excel formula:
=CAUCHY(0.85242, 0, 1, "icdf")
Expected output:
2
Example 4: Median of standard Cauchy distribution
Inputs:
| loc | scale | cauchy_method |
|---|---|---|
| 0 | 1 | median |
Excel formula:
=CAUCHY(0, 1, "median")
Expected output:
0
Python Code
from scipy.stats import cauchy as scipy_cauchy
import math
def cauchy(value=None, loc=0, scale=1, cauchy_method='pdf'):
"""
Wrapper for scipy.stats.cauchy distribution providing multiple statistical methods.
See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.cauchy.html
This example function is provided as-is without any representation of accuracy.
Args:
value (float, optional): Input value for PDF/CDF/SF, or probability for ICDF/ISF methods Default is None.
loc (float, optional): Location parameter for shifting the distribution Default is 0.
scale (float, optional): Scale parameter for stretching the distribution (must be > 0) Default is 1.
cauchy_method (str, optional): Statistical method to compute Valid options: PDF, CDF, ICDF, SF, ISF, Mean, Median, Variance, Std Dev. Default is 'pdf'.
Returns:
float: Distribution result (float), or error message string.
"""
valid_methods = {'pdf', 'cdf', 'icdf', 'sf', 'isf', 'mean', 'median', 'var', 'std'}
if not isinstance(cauchy_method, str):
return f"Invalid method: {cauchy_method}. Must be a string."
method = cauchy_method.lower()
if method not in valid_methods:
return f"Invalid method: {cauchy_method}. Must be one of {sorted(list(valid_methods))}."
try:
loc = float(loc)
scale = float(scale)
except (ValueError, TypeError):
return "Invalid input: loc and scale must be numbers."
if scale <= 0:
return "Invalid input: scale must be > 0."
dist = scipy_cauchy(loc=loc, scale=scale)
try:
if method in {'mean', 'median', 'var', 'std'}:
if method == 'mean':
result = dist.mean()
elif method == 'median':
result = dist.median()
elif method == 'var':
result = dist.var()
elif method == 'std':
result = dist.std()
else:
# Methods that require value
if value is None:
return f"Invalid input: missing required argument 'value' for method '{method}'."
try:
val = float(value)
except (ValueError, TypeError):
return "Invalid input: value must be a number."
if method == 'pdf':
result = dist.pdf(val)
elif method == 'cdf':
result = dist.cdf(val)
elif method == 'sf':
result = dist.sf(val)
elif method in {'icdf', 'isf'}:
if not (0 <= val <= 1):
return f"Invalid input: value (probability) must be between 0 and 1 for {method}."
if method == 'icdf':
result = dist.ppf(val)
else: # isf
result = dist.isf(val)
else:
return f"Internal error: unhandled method {method}"
except Exception as e:
return f"scipy.stats.cauchy error: {str(e)}"
if isinstance(result, float):
if math.isnan(result):
return "Result is NaN (not a number)"
if math.isinf(result):
return "inf" if result > 0 else "-inf"
return result